#######################
# This creates Figure 5, presenting results for those finding actions justified
# Conditional on engage/disengaged and in-party/out-party/apolitical shooter
#######################
#Find mean response for shooter is justified, conditional on in-party shooter and engagement check
x1<-svyby(~justified,~INCORRECT+inparty_shooter,nd,svymean,na.rm=TRUE)

#subset to only in-party shooter condition
x1<-x1[c(3,4),]

#Provide descriptive rather than binary measure of passing engagement check
x1[1,1]<-"Engaged Respondent"
x1[2,1]<-"Disengaged Respondent"

#Find mean response for shooter is justified, conditional only on in-party shooter
x1a<-svyby(~justified,~inparty_shooter,nd,svymean,na.rm=TRUE)

#subset to only in-party shooter condition, name sample
x1a<-cbind("All Respondents",x1a[2,])

#Make column names similar between both sets of results
colnames(x1a)<-c("Sample","ALIGNMENT","justified","se")
colnames(x1)<-c("Sample","ALIGNMENT","justified","se")

#bind conditional and unconditional together for plotting
x1.lines<-rbind(x1,x1a)

#Update values in alignment to more clearly describe shooter
x1.lines$ALIGNMENT<-"In-Party Shooter"

#Find mean response for shooter is justified, conditional on out-party shooter and engagement check
x2<-svyby(~justified,~INCORRECT+outparty_shooter,nd,svymean,na.rm=TRUE)

#sub-set to only out-party condition
x2<-x2[c(3,4),]

#Provide descriptive rather than binary measure of passing engagement check
x2[1,1]<-"Engaged Respondent"
x2[2,1]<-"Disengaged Respondent"

#Find mean response for shooter is justified, conditional only on out-party shooter
x2a<-svyby(~justified,~outparty_shooter,nd,svymean,na.rm=TRUE)

#subset to only out-party shooter condition, name sample
x2a<-cbind("All Respondents",x2a[2,])

#Make column names similar between both sets of results
colnames(x2a)<-c("Sample","ALIGNMENT","justified","se")
colnames(x2)<-c("Sample","ALIGNMENT","justified","se")

#bind conditional and unconditional together for plotting
x2.lines<-rbind(x2,x2a)

#Update values in alignment to more clearly describe shooter
x2.lines$ALIGNMENT<-"Out-Party Shooter"

##Find mean response for shooter is justified, conditional on apolitical shooter and engagement check
x3<-svyby(~justified,~INCORRECT+NP_SHOOTER,nd,svymean,na.rm=TRUE)

#subset to only apolitical shooter
x3<-x3[c(3,4),]

#Provide descriptive rather than binary measure of passing engagement check
x3[1,1]<-"Engaged Respondent"
x3[2,1]<-"Disengaged Respondent"

##Find mean response for shooter is justified, conditional on apolitical shooter and engagement check
x3a<-svyby(~justified,~NP_SHOOTER,nd,svymean,na.rm=TRUE)

#subset to only apolitical shooter, name sample
x3a<-cbind("All Respondents",x3a[2,])

#rename columns to be consistent
colnames(x3)<-c("Sample","ALIGNMENT","justified","se")
colnames(x3a)<-c("Sample","ALIGNMENT","justified","se")

#combine apolitical shooter results
x3.lines<-rbind(x3,x3a)

#Update values in alignment to more clearly describe shooter
x3.lines$ALIGNMENT<-"Apolitical Shooter"

#group all results
xplot.lines<-rbind(x1.lines,x2.lines,x3.lines)

#renumber rows
rownames(xplot.lines)<-c(1:9)

#compute upper and lower limits of 95% confidence intervals
xplot.lines$lower<-xplot.lines$justified-(1.96*xplot.lines$se)
xplot.lines$upper<-xplot.lines$justified+(1.96*xplot.lines$se)

#hard code to make sure no CI is less than 0
xplot.lines$lower<-ifelse(xplot.lines$lower<0,.001,xplot.lines$lower)

#create plot of results
plotlinesjust <- ggplot(xplot.lines, aes(x = justified, y = ALIGNMENT, color=Sample, shape=Sample))+                        
  ggtitle("Suspect is Justified") +
  geom_point(size=2) +
  scale_colour_hue(l=40)+
  xlim(-.01,1)+
  geom_errorbarh(height = 0, aes(xmin = lower, xmax = upper),show.legend=(FALSE))+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(legend.title=element_blank())+
  xlab("Proportion Saying Suspect is Justified (Lines represent 95% CI)") +
  ylab(" ")+
  geom_hline(yintercept=c(1.5,2.5), linetype="dotted")

ggsave("../Main Results/Fig5.tif", width=6, height=4, units = "in",dpi=600)


